﻿Imports System.IO
Imports System.Net

Public Class LibraryManager

    'Still don't know how to download multiple files with a WebClient ...
    Dim WithEvents weclient1 As WebClient = New WebClient()
    Dim WithEvents weclient2 As WebClient = New WebClient()
    Dim WithEvents weclient3 As WebClient = New WebClient()
    Dim WithEvents weclient4 As WebClient = New WebClient()
    Dim WithEvents weclient5 As WebClient = New WebClient()
    Dim WithEvents weclient6 As WebClient = New WebClient()

    Dim dlcount As Integer
    Dim dlccount As Integer

    'Check if file exists
    Private Function CheckAddress(ByVal URL As String) As Boolean
        Try
            Dim request As WebRequest = WebRequest.Create(URL)
            Dim response As WebResponse = request.GetResponse()
        Catch ex As Exception
            Return False
        End Try
        Return True
    End Function

    Private Sub Check_FilesAndFolder()

        'Check if Auto updating is set to true to set the download count
        If Library.AutoUpdating = True Then
            UpdateBar.Maximum = Library.installedlibrarys
            dlccount = Library.installedlibrarys
        End If

        'The library folder is necessary to install the libs
        If Not Directory.Exists(".\librarys") Then
            Directory.CreateDirectory(".\librarys")
        End If

        'Check if file exists and mark them as installed
        If File.Exists(".\librarys\ps2-lib.dll") Then
            PS2ChBox.Checked = True
            PS2ChBox.Enabled = False
        End If

        If File.Exists(".\librarys\ps3-lib.dll") Then
            PS3ChBox.Checked = True
            PS3ChBox.Enabled = False
        End If

        If File.Exists(".\librarys\ps4-lib.dll") Then
            PS4ChBox.Checked = True
            PS4ChBox.Enabled = False
        End If

        If File.Exists(".\librarys\psp-lib.dll") Then
            PSPChBox.Checked = True
            PSPChBox.Enabled = False
        End If

        If File.Exists(".\librarys\psv-lib.dll") Then
            PSVChBox.Checked = True
            PSVChBox.Enabled = False
        End If

        If File.Exists(".\librarys\tools-lib.dll") Then
            ToolsChBox.Checked = True
            ToolsChBox.Enabled = False
        End If

    End Sub

    Private Sub Update_librarys()

        'If you're the first time running PS Multi Tools then you have no librarys, so don't update because there's nothing to update
        If Library.installedlibrarys = 0 Then
            Library.AutoUpdating = False
            Exit Sub
        End If

        Me.Cursor = Cursors.WaitCursor

        If PS2ChBox.Checked Then
            weclient1.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps2/ps2-lib.dll"), ".\librarys\ps2-lib.dll")
        End If

        If PS3ChBox.Checked Then
            weclient2.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps3/ps3-lib.dll"), ".\librarys\ps3-lib.dll")
        End If

        If PS4ChBox.Checked Then
            weclient3.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps4/ps4-lib.dll"), ".\librarys\ps4-lib.dll")
        End If

        If PSPChBox.Checked Then
            weclient4.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/psp/psp-lib.dll"), ".\librarys\psp-lib.dll")
        End If

        If PSVChBox.Checked Then
            weclient5.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/psv/psv-lib.dll"), ".\librarys\psv-lib.dll")
        End If

        If ToolsChBox.Checked Then
            weclient6.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/tools/tools-lib.dll"), ".\librarys\tools-lib.dll")
        End If

    End Sub

    Private Sub LibraryManager_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'Check installed librarys and "library" folder
        Check_FilesAndFolder()
    End Sub

    Private Sub LibraryManager_Shown(sender As Object, e As EventArgs) Handles Me.Shown
        'Check if auto-update
        If Library.AutoUpdating = True Then
            InstallButton.Visible = False
            UpdateBar.Visible = True

            Library.Clear_Old_Lib()
            Update_librarys() 'Could throw an error when updating while you have opened a library
        End If
    End Sub

    Private Sub InstallButton_Click(sender As Object, e As EventArgs) Handles InstallButton.Click

        Me.Cursor = Cursors.WaitCursor

        'Check what to install and add to download count
        If PS2ChBox.Checked And PS2ChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            weclient1.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps2/ps2-lib.dll"), ".\librarys\ps2-lib.dll")
        End If

        If PS3ChBox.Checked And PS3ChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            weclient2.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps3/ps3-lib.dll"), ".\librarys\ps3-lib.dll")
        End If

        If PS4ChBox.Checked And PS4ChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            weclient3.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/ps4/ps4-lib.dll"), ".\librarys\ps4-lib.dll")
        End If

        If PSPChBox.Checked And PSPChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            If CheckAddress("http://85.31.189.150/multitools/psp/psp-lib.dll") = True Then
                weclient4.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/psp/psp-lib.dll"), ".\librarys\psp-lib.dll")
            Else
                MsgBox("The PSP Library is currently not available.", MsgBoxStyle.Information, "PSP can not be installed/updated!")
            End If
        End If

        If PSVChBox.Checked And PSVChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            If CheckAddress("http://85.31.189.150/multitools/psv/psv-lib.dll") = True Then
                weclient5.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/psv/psv-lib.dll"), ".\librarys\psv-lib.dll")
            Else
                MsgBox("The PSV Library is currently not available.", MsgBoxStyle.Information, "PSV can not be installed/updated!")
            End If
        End If

        If ToolsChBox.Checked And ToolsChBox.Enabled = True Then
            UpdateBar.Maximum += 1
            dlccount += 1
            weclient6.DownloadFileAsync(New Uri("http://85.31.189.150/multitools/tools/tools-lib.dll"), ".\librarys\tools-lib.dll")
        End If

        MsgBox(UpdateBar.Maximum.ToString + vbNewLine + dlccount.ToString)

    End Sub

    Private Sub Check_DL()
        'Occurs when installing a library
        If dlcount = dlccount And Not Library.AutoUpdating = True Then
            If MsgBox(INIs.GetStringOfLang("Library Manager", "Installed").ToString.Split("|")(0), MsgBoxStyle.Information, INIs.GetStringOfLang("Library Manager", "Installed").ToString.Split("|")(1)) = MsgBoxResult.Ok Then
                Library.Check_Librarys()
                Me.Close()
            End If
        End If

        'Occurs when updating a library
        If UpdateBar.Value = UpdateBar.Maximum And Library.AutoUpdating = True Then
            Me.Cursor = Cursors.Default
            Library.AutoUpdating = False

            If MsgBox(INIs.GetStringOfLang("Library Manager", "Updated").ToString.Split("|")(0), MsgBoxStyle.Information, INIs.GetStringOfLang("Library Manager", "Updated").ToString.Split("|")(1)) = MsgBoxResult.Ok Then
                Library.Check_Librarys()
                Me.Close()
            End If
        End If
    End Sub

    Private Sub weclient1_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient1.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

    Private Sub weclient2_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient2.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

    Private Sub weclient3_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient3.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

    Private Sub weclient4_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient4.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

    Private Sub weclient5_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient5.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

    Private Sub weclient6_DownloadFileCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles weclient6.DownloadFileCompleted
        dlcount = dlcount + 1
        UpdateBar.Value += 1

        Check_DL()
    End Sub

End Class